package com.atguigu.yygh.hosp.controler.api;


import com.atguigu.yygh.cmn.common.exception.YyghException;
import com.atguigu.yygh.cmn.common.helper.HttpRequestHelper;
import com.atguigu.yygh.cmn.common.result.Result;
import com.atguigu.yygh.cmn.common.result.ResultCodeEnum;
import com.atguigu.yygh.cmn.common.util.MD5;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.hosp.service.HospitalService;
import com.atguigu.yygh.hosp.service.HospitalSetService;

import com.atguigu.yygh.hosp.service.ScheduleService;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.model.hosp.Schedule;
import com.atguigu.yygh.vo.hosp.DepartmentQueryVo;

import com.atguigu.yygh.vo.hosp.ScheduleQueryVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;


@RestController
@RequestMapping("/api/hosp")
public class ApiControler {
    @Autowired
    private HospitalService hospitalService;
    @Autowired
    private HospitalSetService hospitalSetService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private ScheduleService scheduleService;

    @ApiOperation(value = "上传医院")
    @PostMapping("saveHospital")
    public Result saveHospital(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
//        获取签名密钥
        String sign = (String) paramMap.get("sign");
        System.out.println("请求的签名sign:"+sign);
//        通过hoscode获取数据库的签名密钥并加密
        String hoscode = paramMap.get("hoscode").toString();
        String signKey = hospitalSetService.getSignKey(hoscode);
        String signKeyMd5 = MD5.encrypt(signKey);
//        System.out.println("进行签名对比,请求hoscode:"+hoscode+"得到signKey:"+signKey);
//        System.out.println("获取到的加密数据signKeyMd5:"+signKeyMd5);
//        System.out.println(sign.equals(signKeyMd5));
//        对比两个签名密钥
        if (!sign.equals(signKeyMd5)){
            System.out.println("签名不一样不可以进行调用！！！");
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        System.out.println("签名通过成功");
//        将图片数据进行处理
        String logoData = paramMap.get("logoData").toString();
        logoData=logoData.replaceAll(" ","+");
        paramMap.put("logoData",logoData);
//        进行保存
        hospitalService.save(paramMap);
        return Result.ok();
    }
    @ApiOperation("通过hoscode找医院")
    @PostMapping("hospital/show")
    public Result getByHoscode(HttpServletRequest request ){
         Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
//        获取签名密钥
        String sign = (String) paramMap.get("sign");
        String hoscode = paramMap.get("hoscode").toString();
        String signKey = hospitalSetService.getSignKey(hoscode);
        String signKeyMd5 = MD5.encrypt(signKey);
//        对比两个签名密钥
        if (!sign.equals(signKeyMd5)){
            System.out.println("签名不一样不可以进行调用！！！");
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
//        根据编号查医院数据
        Hospital hospital= hospitalService.getByHoscode(hoscode);
        return Result.ok(hospital);
    }
//    上传科室接口
    @ApiOperation(value = "上传科室")
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request){
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //        获取签名密钥
//        paramMap.get("")
        String sign = (String) paramMap.get("sign");
        System.out.println("请求的签名密钥:"+sign);
        String hoscode = paramMap.get("hoscode").toString();
        String signKey = hospitalSetService.getSignKey(hoscode);
        String signKeyMd5 = MD5.encrypt(signKey);
        System.out.println("数据库的签名密钥:"+signKey);
        System.out.println("数据库的md5签名密钥:"+signKeyMd5);
//        对比两个签名密钥
        if (!sign.equals(signKeyMd5)){
            System.out.println("签名不一样不可以进行调用！！！");
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
//        进行保存
        departmentService.save(paramMap);
        return Result.ok();
    }
    
    @ApiOperation(value = "获取分页列表")
    @PostMapping("department/list")
    public Result department(HttpServletRequest request){
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //        获取签名密钥
        String hoscode = paramMap.get("hoscode").toString();
        String depcode = (String)paramMap.get("depcode");

        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String)paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 10 : Integer.parseInt((String)paramMap.get("limit"));

        if(StringUtils.isEmpty(hoscode)) {
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }

        //签名校验
        String sign = (String) paramMap.get("sign");
        System.out.println("请求的签名密钥:"+sign);
        if (! sign.equals(MD5.encrypt(hospitalSetService.getSignKey(hoscode)))){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        // 根据
        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);
        departmentQueryVo.setDepcode(depcode);
        Page<Department> pageModel = departmentService.selectPage(page, limit, departmentQueryVo);
        System.out.println(HttpRequestHelper.isSignEquals(paramMap, hospitalSetService.getSignKey(hoscode)));
        System.out.println(paramMap);
        System.out.println(hospitalSetService.getSignKey(hoscode));
        return Result.ok(pageModel);
    }

    @ApiOperation(value = "删除科室")
    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
//必须参数校验 略
        String hoscode = (String)paramMap.get("hoscode");
//必填
        String depcode = (String)paramMap.get("depcode");
        if(StringUtils.isEmpty(hoscode)) {
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
//签名校验
        String sign = (String) paramMap.get("sign");
        System.out.println("请求的签名密钥:"+sign);
        if (! sign.equals(MD5.encrypt(hospitalSetService.getSignKey(hoscode)))){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        departmentService.remove(hoscode, depcode);
        return Result.ok();
    }
    @ApiOperation(value = "上传排班")
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request){
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //必须参数校验 略
        String hoscode = (String)paramMap.get("hoscode");
        if(StringUtils.isEmpty(hoscode)) {
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        //签名校验
        String sign = (String) paramMap.get("sign");
        System.out.println("请求的签名密钥:"+sign);
        if (! sign.equals(MD5.encrypt(hospitalSetService.getSignKey(hoscode)))){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        scheduleService.save(paramMap);
        return Result.ok();
    }

    @ApiOperation(value = "获取排班分页列表")
    @PostMapping("schedule/list")
    public Result schedule(HttpServletRequest request){
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //必须参数校验 略
        String hoscode = (String)paramMap.get("hoscode");
        if(StringUtils.isEmpty(hoscode)) {
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        //签名校验
        String sign = (String) paramMap.get("sign");

        if (! sign.equals(MD5.encrypt(hospitalSetService.getSignKey(hoscode)))){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        String depcode = (String)paramMap.get("depcode");
        Integer page = StringUtils.isEmpty(paramMap.get("page")) ? 1:Integer.parseInt((String)paramMap.get("page"));
        Integer limit = StringUtils.isEmpty(paramMap.get("limit")) ? 10:Integer.parseInt((String)paramMap.get("limit"));
        ScheduleQueryVo scheduleQueryVo=new ScheduleQueryVo();
        scheduleQueryVo.setDepcode(depcode);
        scheduleQueryVo.setHoscode(hoscode);
        Page<Schedule> page1=  scheduleService.selectPage(page,limit,scheduleQueryVo);
        return Result.ok(page1);
    }
    @ApiOperation(value = "删除科室")
    @PostMapping("schedule/remove")
    public Result removeSchedule(HttpServletRequest request){
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //必须参数校验 略
        String hoscode = (String)paramMap.get("hoscode");
        if(StringUtils.isEmpty(hoscode)) {
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        //签名校验
        String sign = (String) paramMap.get("sign");
        System.out.println("请求的签名密钥:"+sign);
        if (! sign.equals(MD5.encrypt(hospitalSetService.getSignKey(hoscode)))){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        String hosScheduleId = (String)paramMap.get("hosScheduleId");
        scheduleService.remove(hoscode,hosScheduleId);
        return Result.ok();
    }
}
